//
//  ICJSBridgeManager.m
//  ICome
//
//  Created by zhangrongwu on 16/5/24.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICJSBridgeTools.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import<CoreTelephony/CTCarrier.h>
#import "ICMediaManager.h"
#import "ICFileTool.h"
#import "NSDate+Extension.h"
#import "ICApplicationNetworkManager.h"

@implementation ICJSBridgeTools

//获取操作系统平台，iOS或AndroidCTTelephonyNetworkInfo
+(NSString *)getOS {
    NSString *userAgent = [NSString stringWithFormat:@"; %@/%@;%@%.2f;iOS;%@",[ICTools getDeviceTypeInfo],[ICTools getAppVersion],Device,SystemVersion,DeviceType];
    return userAgent;
}

//判断当前环境是否支持iComJS，该依据是通过iCom轻应用专属的UserAgent值来判断
+(NSString *)isICom {
    return @"";
}

//关闭当前界面
+(void)closeVC:(ICBaseViewController *)VC {
    [VC.navigationController popViewControllerAnimated:YES];
}

//获取网络状态 网络类型：wifi、2g、3g、4g、unknown、none（none表示离线)
+(NSString *)getNetworkType {
    return [ICTools getNetWorkType];
}

//获取用户信息data: {userName:用户姓名(string)avatar:用户头像地址(string)gender:用户性别(int)userID:用户账号(string)deviceUUID: 设备UUID(string)}
+(NSDictionary *)getUserInfo {
    NSDictionary *info = @{@"photoId":[ICUser currentUser].photoId == nil ? @"" :[ICUser currentUser].photoId,
                           @"userId":[ICUser currentUser].eId,
                           @"account":[ICUser currentUser].account,
                           @"userName":[ICUser currentUser].eName,
                           @"deviceId":[ICTools getDeviceId]};
    return info;
}

+ (NSString *)typeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];
    switch (c) {
        case 0xFF:
            return @"image/jpeg";
        case 0x89:
            return @"image/png";
        case 0x47:
            return @"image/gif";
        case 0x49:
        case 0x4D:
            return @"image/tiff";
            
    }
    return nil;
}


+ (NSString *)appendUserInfoforJSURL:(NSString *)URL {
    NSString *newUrl = @"";
    NSURL *url = [NSURL URLWithString:URL];
    if (![ICTools stringEmpty:url.query]) {// icom://baidu.com?ticket=jsticket
        newUrl = [NSString stringWithFormat:@"%@&ticket=%@", URL,[ICTools getJSTicket]];
    } else {
        newUrl = [NSString stringWithFormat:@"%@?ticket=%@", URL,[ICTools getJSTicket]];
    }
    return newUrl;
}

+(NSString *)replacingJSTicketJSURL:(NSString *)URL {
    NSString *newUrl = @"";
    if (![ICTools stringEmpty:URL]) {
        
        newUrl = [URL stringByReplacingOccurrencesOfString:@"ticket=ticket" withString:[NSString stringWithFormat:@"ticket=%@", [ICTools getJSTicket]]];
    }
    return newUrl;
}

+(void)getJsTicket:(void (^)(BOOL isSuccess, id object))success {
    NSDictionary *param = @{@"eId":[ICUser currentUser].eId == nil ? @"":[ICUser currentUser].eId};
    [[ICApplicationNetworkManager sharedInstance] getJsTicketWithObject:param success:^(id object) {
        [[NSUserDefaults standardUserDefaults] setObject:[[object objectForKey:@"data"] objectForKey:@"ticket"] forKey:@"JSTicket"];
        [ICNetworkHelper setValue:[ICTools getJSTicket] forHTTPHeaderField:@"auth"];
        if (success) success(YES, object);
    } failure:^(NSError *error) {
        if (success) success(NO, nil);
    }];
}

@end
